﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V2.PaidTimeOffDAL;
using System.Data.Linq;
using System.Collections.Generic;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        HRPaidTimeOffDataContext db = new HRPaidTimeOffDataContext();
        ENTUserAccount userAccount = db.ENTUserAccounts.Single(ua => ua.WindowsAccountName == @"VARALLO1\VaralloMadison");

        ViewState["ENTUserAccountId"] = userAccount.ENTUserAccountId;
        ViewState["InsertENTUserAccountId"] = userAccount.InsertENTUserAccountId;
        ViewState["InsertDate"] = userAccount.InsertDate;
        ViewState["Version"] = userAccount.Version;
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        //Create an instance of the data context
        HRPaidTimeOffDataContext db = new HRPaidTimeOffDataContext();

        //int entUserAccountId = db.ENTUserAccountInsert(@"VARALLO1\VaralloMadison", "Madison", "Varallo", "madison.varallo@v2.com",
        //    true, 1);

        //Create a new ENTUserAccount object and set the properties
        ENTUserAccount userAccount = new ENTUserAccount
        {
            WindowsAccountName = @"VARALLO1\VaralloMadison",
            FirstName = "Madison",
            LastName = "Varallo",
            Email = "madison.varallo@v2.com",
            IsActive = true,
            InsertDate = DateTime.Now,
            InsertENTUserAccountId = 1,
            UpdateDate = DateTime.Now,
            UpdateENTUserAccountId = 1
        };

        //Signal the context to insert this record
        db.ENTUserAccounts.InsertOnSubmit(userAccount);
        //Save the changes to the database
        db.SubmitChanges();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //Create a ENTUserAccount object and set the properties
        ENTUserAccount userAccount = new ENTUserAccount
        {            
            WindowsAccountName = @"VARALLO1\VaralloMadison",
            FirstName = "Madison",
            LastName = "Varallo",
            Email = "madison.varallo@v2.com",
            IsActive = false,            
            UpdateDate = DateTime.Now,
            UpdateENTUserAccountId = 1
        };

        userAccount.ENTUserAccountId = Convert.ToInt32(ViewState["ENTUserAccountId"]);
        userAccount.Version = (Binary)ViewState["Version"];
        
        HRPaidTimeOffDataContext db = new HRPaidTimeOffDataContext();
        db.ENTUserAccounts.Attach(userAccount, true);        
        db.SubmitChanges();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        HRPaidTimeOffDataContext db = new HRPaidTimeOffDataContext();

        //Create a ENTUserAccount object and set the properties
        ENTUserAccount userAccount = new ENTUserAccount();

        userAccount.ENTUserAccountId = Convert.ToInt32(ViewState["ENTUserAccountId"]);
        userAccount.Version = (Binary)ViewState["Version"];

        db.ENTUserAccounts.Attach(userAccount);
        db.ENTUserAccounts.DeleteOnSubmit(userAccount);
        db.SubmitChanges();
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        HRPaidTimeOffDataContext db = new HRPaidTimeOffDataContext();
                
//        GridView1.DataSource = db.ENTUserSelectAll();
        GridView1.DataBind();
    }
    protected void btnCreateError_Click(object sender, EventArgs e)
    {        
        int x = 0;
        try
        {
            x = 0;
            int y = 5 / x;
        }
        catch (DivideByZeroException divByZeroEx)
        {
            throw new Exception("You attempted to divide by zero.  Try again!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            
        }
        

    }

    //public void Page_Error(object sender, EventArgs e)
    //{
    //    if (!EventLog.SourceExists("PaidTimeOff"))
    //    {
    //        EventLog.CreateEventSource("PaidTimeOff", "Application");
    //    }
    //    EventLog eventLog = new EventLog();
    //    eventLog.Log = "Application";
    //    eventLog.Source = "PaidTimeOff";
    //    eventLog.WriteEntry(Server.GetLastError().GetBaseException().ToString(), EventLogEntryType.Error);
    //    Response.Redirect("ErrorPage.aspx");
    //}
}
